package com.ssm.property.server.controller.residence;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.support.Const;
import com.jee.ssm.common.utils.MD5;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.property.dao.model.building.Building;
import com.ssm.property.dao.model.residence.Residence;
import com.ssm.property.dao.model.room.Room;
import com.ssm.property.dao.model.unit.Unit;
import com.ssm.property.rpc.api.building.BuildingService;
import com.ssm.property.rpc.api.residence.ResidenceService;
import com.ssm.property.rpc.api.room.RoomService;
import com.ssm.property.rpc.api.unit.UnitService;
import com.ssm.upms.dao.model.Account;
import com.ssm.upms.dao.model.AccountRole;
import com.ssm.upms.log.AdminControllerLog;
import com.ssm.upms.rpc.api.ssm.AccountRoleService;
import com.ssm.upms.rpc.api.ssm.AccountService;
import com.ssm.upms.rpc.api.ssm.CityService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.*;

/**
* 小区管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/residence")
public class
ResidenceController  {


    /**
     * 进入小区添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("residence:add")
    public String add(HttpServletRequest request, ModelMap model){
        ParamMap paramMap=new ParamMap(request);
        paramMap.put("parentId",request.getParameter("parentId"));
     /*   model.put("provinces", cityService.chooseNext(paramMap));*/
        model.put("longId", UUIDFactory.getStringId());
        return "manager/residence/add";
    }
/*

    */
/**
     * 查看小区管理员的信息
     * @param residenceId 小区的id
     * @return 小区管理员页面
     *//*

    @RequestMapping(value = "/resAdminList")
    public  String resAdminList(@RequestParam String residenceId, ModelMap model, HttpSession session){
        model.put("residenceId",residenceId);
        List<Account> accountList=accountService.selectByInfoId(residenceId);
        if(accountList.size()!=0){
        //String infoId=accountList.get(0).getInfoId().split(",")[0];
        //residenceService.selectById(residenceId);
        for (Account a:accountList){
            //a.setEstate(estateService.selectById(infoId));
            a.setResidence(residenceService.selectById(a.getInfoId().split(",")[1]));
            }
        }
        model.put("accountList",accountList);
        return "manager/residence/resAdminList";
    }
*/

    /**
     * 进入小区管理员添加页面，携带一个声称的id
     * @param model 返回实体容器
     * @return 添加页面
     */
    @RequestMapping(value = "/addResAdmin")
    @RequiresPermissions("residence:add")
    public String addResAdmin(ModelMap model, @RequestParam String  residenceId){
        model.put("longId", UUIDFactory.getStringId());
        model.put("residenceId",residenceId);
        return "manager/residence/addResAdmin";

    }

    /**
     * 进入小区编辑页面
     * @param model 返回小区的容器
     * @param id 小区id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("residence:edit")
    public String edit(HttpServletRequest request, ModelMap model, @RequestParam String id) {
     /*   model.put("data",residenceService.selectById(id));
        ParamMap paramMap=new ParamMap(request);
        paramMap.put("parentId",1);
        model.put("provinces", cityService.chooseNext(paramMap));
        paramMap.put("parentId",cityService.selectByName(residenceService.selectById(id).getProvince()).getId());
        model.put("city", cityService.chooseNext(paramMap));
         paramMap.put("parentId",cityService.selectByName(residenceService.selectById(id).getCity()).getId());
         model.put("county", cityService.chooseNext(paramMap));*/
        return "manager/residence/edit";
    }

    /**
     * 小区添加
     * @param residence 带id的小区对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("residence:add")
    @AdminControllerLog(description = "添加小区" )
    public Tip save(Residence residence, HttpSession session, HttpServletRequest request)  {

            try {
                Account account = (Account)session.getAttribute(Const.ACCOUNT);
                ParamMap paramMap=new ParamMap(request);
                paramMap.put("estateId",account.getInfoId());
                paramMap.put("code",residence.getCode());
              if(residenceService.countCode(paramMap) !=0)  {
                  return  new Tip(1,"小区编号已存在");
              }
                residence.setEstateId(account.getInfoId());
                residenceService.insert(residence);
                return  new Tip();
            } catch (Exception e) {
                return new Tip(1,"添加失败！");
            }
    }

    /**
     * 小区管理员的保存
     * @param account 物业对象
     * @return 成功状态
     */
/*    @RequestMapping(value = "/saveResAdmin")
    @ResponseBody
    public  Tip saveResAdmin(Account account, HttpSession session){
        try {
                Account currentAccount= (Account) session.getAttribute(Const.ACCOUNT);
                account.setId(UUIDFactory.getStringId());
                account.setType(21);
                account.setPassword(MD5.md5(account.getPassword()));
                account.setInfoId(currentAccount.getInfoId()+","+account.getResidenceId());
                accountService.insert(account);

                AccountRole accountRole=new AccountRole();
                accountRole.setId(UUIDFactory.getStringId());
                accountRole.setAccountId(account.getId());
                accountRole.setRoleId("937873834136783355");
                accountRoleService.insert(accountRole);
              return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"添加失败！");
        }
    }*/

    /**
     * 根据 id 修改小区
     * @param residence 带id的小区对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("residence:edit")
    @AdminControllerLog(description = "修改小区" )
    public Tip update(Residence residence, HttpServletRequest request, HttpSession session) {
            try {
                if (!residenceService.selectById(request.getParameter("id")).getCode().equals(residence.getCode())) {
                    Account account = (Account)session.getAttribute(Const.ACCOUNT);
                    ParamMap paramMap = new ParamMap(request);
                    paramMap.put("estateId", account.getInfoId());
                    paramMap.put("code", residence.getCode());
                    if (residenceService.countCode(paramMap) > 0) {
                        return new Tip(1, "小区编号已存在");
                    }
                }
                residenceService.updateById(residence);
                return new Tip();
            } catch (Exception e) {
                //e.printStackTrace();
                return new Tip(1, "修改失败！");
            }
    }

    /**
     * 根据 id 删除小区
     * @param id 小区id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("residence:delete")
    @AdminControllerLog(description = "删除小区" )
    public Tip delete(@RequestParam String id) {
            try {
            residenceService.deleteById(id);
            buildingService.deleteByResidenceId(id);
            unitService.deleteByResidenceId(id);
            roomService.deleteByResidenceId(id);
         /*   userRoomService.deleteByResidenceId(id);
            cardService.deleteByResidenceId(id);
           List<Content> contents= contentService.selectByResidenceId(id);
           if(contents.size()>0){
               for(Content c:contents){
                  replyService.deleteByContentId(c.getId());
               }
               contentService.deleteByResidenceId(id);
           }
            List<Equipment> equipmentList = equipmentService.selectByResidenceId(id);//查询小区下的所有设备
                if(equipmentList!=null && !equipmentList.isEmpty()){
                    for (Equipment equipment:equipmentList) {
                        equipmentCardService.deleteByEquipmentId(equipment.getId());//根据设备id 删除
                    }
                }
                equipmentService.deleteByResidenceId(id);*/
                return new Tip();
            } catch (Exception e) {
                //e.printStackTrace();
                return new Tip(1, "删除失败！");
            }
    }

    /**
     *根据id删除信息
     * @param id
     * @return
     */
    @RequestMapping(value="/deleteResAdmin")
    @ResponseBody
    public Tip deleteResAdmin(@RequestParam String id){
        try {
            accountRoleService.deleteByAccountId(id);
            accountService.deleteById(id);
            return  new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"删除失败");
        }
    }

    /**
     * 根据 id 列表批量删除
     * @param ids 小区id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("residence:delete")
    @AdminControllerLog(description = "批量删除小区" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {
        try {
            residenceService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据小区的id查询小区的信息
     * @return
     */
    @RequestMapping(value = "/selcetRes")
    public List<Residence> selcetRes(@RequestParam("residenceIds") List<String> residenceIds){
        List<Residence> residenceList=new ArrayList<Residence>();
        for(String residenceId:residenceIds){
            System.out.println("选择小区的"+residenceId);
           residenceList.add(residenceService.selectById(residenceId));
        }
        return residenceList;
    }


    /**
     * 根据policeId获取所有权限，并且显示那些属于这个的小区
     * 用于角色授权
     * @param policeId  派出所Id
     * @return 页面
     */
/*    @RequestMapping(value = "/findByPoliceInfo")
    public String findByPoliceId(String policeId,ModelMap modelMap){
        List<Estate> estateList = estateService.selectAll(null);
        ParamMap paramMap=new ParamMap();
        for(Estate e : estateList) {
            paramMap.put("estateId",e.getId());
            e.setResidenceList(residenceService.findByPoliceId(paramMap));
        }
        modelMap.put("estateList", estateList);
        ParamMap p=new ParamMap();
        p.put("type",1);
        List<Account>  accountList=accountService.lisrAccount(p);
        for(Account a:accountList){
            paramMap.put("estateId",a.getInfoId());
            a.setResidences(residenceService.findByPoliceId(paramMap));
        }
        modelMap.put("accountList", accountList);
        return  "manager/police/choosePower";
    }*/



    /**
     * 根据 id 查找小区
     * @param id 小区id
     * @return 小区对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("residence:list")
    public Residence find(@RequestParam String id) {

        return residenceService.selectById(id);
    }


    /**
     * 获取小区列表 获取全部 不分页
     * @param request 请求参数
     * @return 小区列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("residence:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {
        return list(request,modelMap,1,0);
    }




    /**
     * 获取小区树结构
     * @param request 请求参数
     * @return 小区列表页面
     */
   /* @RequestMapping(value="/tree")
    public String tree(HttpServletRequest request, ModelMap modelMap) {
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        modelMap.put("ttyyppee",account.getType());
       if(account.getType()==5){
           List<PoliceResidence> policeResidenceList= policeResidenceService.findByPoliceId(account.getInfoId());
           List<Residence> resList=new ArrayList<Residence>();
               for(PoliceResidence pr:policeResidenceList){
                   resList.add(residenceService.selectById(pr.getResidenceId()));
               }
           if(resList.size()>0){
               for (Residence res :resList){
                   List<Building> buildings = buildingService.chooseBuidling(res.getId());//该小区下的楼栋
                   if(buildings.size()>0) {
                       res.setBuildings(buildings);
                       for (Building b : buildings) {
                           List<Unit> unit = unitService.findByBuildingId(b.getId());
                           if (unit.size() > 0) {
                               for (Unit u : unit) {
                                   u.setEquipment(equipmentService.selectByUnitId(u.getId()));
                               }
                           }
                           b.setUnits(unit);
                       }
                   }
               }
               modelMap.put("residence",resList);
               return "manager/residence/findInfo";
           }
       }
       else if(account.getType()==1){
            Estate estate =new Estate();
            List<Estate> liste =estateService.selectAll(estate);
           if(liste.size()>0){
               for (Estate es :liste){
                   Residence re =new Residence();
                   List<Residence>list =residenceService.listAll(re);
                   if(list.size()>0){
                       es.setResidenceList(list);
                       for (Residence res :list){
                           List<Building> buildings = buildingService.chooseBuidling(res.getId());//该小区下的楼栋
                           if(buildings.size()>0) {
                               res.setBuildings(buildings);
                               for (Building b : buildings) {
                                   List<Unit> unit = unitService.findByBuildingId(b.getId());
                                   if (unit.size() > 0) {
                                       for (Unit u : unit) {
                                           u.setEquipment(equipmentService.selectByUnitId(u.getId()));
                                       }
                                   }
                                   b.setUnits(unit);
                               }
                           }
                       }
                       modelMap.put("es",liste);
                       return "manager/residence/tree";
                   }
               }

           }
           modelMap.put("es",liste);
           return "manager/residence/tree";
        }

        return "manager/residence/tree";
    }




*/



    /**
     * 获取小区列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 小区列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("residence:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap = new ParamMap(request);
        if(account.getType() == 2) {
            paramMap.put("estateId", account.getInfoId());
        } else if(account.getType()==21){
            String residenceId = account.getInfoId().split(",")[1];
            paramMap.put("residenceId",residenceId);
            /*//小区人数
            Long count = userService.countId(residenceId);
            modelMap.put("count",count);
            //男性数量
            Long countMale = userService.countMale(residenceId);
            modelMap.put("countMale",countMale);
            //女性数量
            Long countFemale = userService.countFemale(residenceId);
            modelMap.put("countFemale",countFemale);
            //60以上
            Long countOld = userService.countOld(residenceId);
            modelMap.put("countOld",countOld);
            //16以下
            Long countChild = userService.countChild(residenceId);
            modelMap.put("countChild",countChild);*/
        } /*else if(account.getType()==5){
           List<PoliceResidence> policeResidenceList= policeResidenceService.findByPoliceId(account.getInfoId());
            List<String> policeIdList=new ArrayList<String>();
           if(policeResidenceList.size() > 0){
               for(PoliceResidence pr:policeResidenceList){
                   policeIdList.add(pr.getResidenceId());
               }
           } else {
               policeIdList.add("0");
           }
           paramMap.put("policeIdList", policeIdList);
        }*/
        modelMap.put("account",account.getType());
        modelMap.put("pageInfo",residenceService.list(paramMap,page,size));
        modelMap.put("type",account.getType());
        modelMap.putAll(paramMap);
        return "manager/residence/list";
    }
    /**
     * 获取小区列表 分页
     * @param request 请求参数
     * @return 小区列表页面
     */
    @RequestMapping(value="/checklist")
    @RequiresPermissions("residence:list")
    public String checklist(HttpServletRequest request, ModelMap modelMap) {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap = new ParamMap(request);
        if (account.getType() == 2) {
            paramMap.put("estateId", account.getInfoId());
        }else if(account.getType()==21){
            paramMap.put("estateId", account.getInfoId().split(",")[0]);
            paramMap.put("residenceId", account.getInfoId().split(",")[1]);
        }
        modelMap.put("pageInfo",residenceService.list(paramMap));
        return "manager/residence/chooseResidence";
    }

    /**
     * 获取小区列表(可以多选) 分页
     * @param request 请求参数
     * @return 小区列表页面
     */
    @RequestMapping(value="/checklists")
    @RequiresPermissions("residence:list")
    public String checklists(HttpServletRequest request, ModelMap modelMap, @RequestParam String estateId) {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap = new ParamMap(request);
        if(account.getType()==5){
            paramMap.put("estateId", estateId);
        }
        modelMap.put("estateId",estateId);
        modelMap.put("pageInfo",residenceService.list(paramMap));
        return "manager/residence/chooseResidences";
    }

    /**
     * 根据不同用户首页地图查询加载的小区
     * @param session
     * @return
     */
    @RequestMapping(value = "/listAll")
    @ResponseBody
    public List<Residence> listAll(HttpSession session, ParamMap p) {
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        if(account.getType() == 2) {
            p.put("estateId", account.getInfoId());
        } else if(account.getType() == 21) {
            p.put("id", account.getInfoId().split(",")[1]);
        }/* else if(account.getType() == 5) {
            //查询派出所所辖小区
            List<PoliceResidence> l = policeResidenceService.findByPoliceId(account.getInfoId());
            if(l.size() > 0) {
                List<String> ls = new ArrayList<>();
                for(PoliceResidence pr : l) {
                    ls.add(pr.getResidenceId());
                }
                //拼接小区id放入list集合
                p.put("ids", ls);
            } else {
                return null;
            }
        }*/
        List<Residence> list = residenceService.listByAccount(p);
        return list;
    }

    /**
     * 根据小区id查询小区详细信息，包括小区的统计信息
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/findResidenceInfo")
    public String findResidenceInfo(String id, ModelMap modelMap) {
        Residence residence = residenceService.selectById(id);
        List<Building> buildings = buildingService.findByResidenceId(id);
        List<Unit> units = unitService.findByResidenceId(id);
        List<Room> rooms = roomService.findByResidenceId(id);
        modelMap.put("residence", residence);
        modelMap.put("buildingTotal", buildings.size());
        modelMap.put("unitTotal", units.size());
        modelMap.put("roomTotal", rooms.size());
        return "manager/residence/residence-info";
    }

    /**
     * 省内小区数量
     * @return
     */
/*
    @RequestMapping(value = "/countName")
    @ResponseBody
    public Map<String, Object> countName(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Residence residence = new Residence();
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute(Const.ACCOUNT);
        if (account.getType() == 2) {
            residence.setEstateId(account.getInfoId());
        }else if(account.getType() == 21){
            residence.setId(account.getInfoId().split(",")[1]);
        }else if(account.getType() == 5){
            residence.setBelong(account.getInfoId());
        }

        List<Residence> queryCommunityForList = residenceService.countName(residence);
        int length = queryCommunityForList.size();
        String[] nameArray = new String[length];
        List<Echat> list = new ArrayList<Echat>();
        for(int i = 0 ;i<nameArray.length; i++  ){
            nameArray [i] = queryCommunityForList.get(i).getProvince();  //获取省份
        }
        for(Residence community : queryCommunityForList){
            residence.setProvince(community.getProvince());
            Residence re = residenceService.countNumber(residence);
            Echat echat= new Echat();
            echat.setValue(re.getProvince());
            echat.setName(community.getProvince());
            list.add(echat);
        }
        map.put("value",list);
        map.put("name",nameArray);
        return map;
    }
*/


    /**
     * 查询小区管理员列表
     * @return
     */
  /*  @RequestMapping(value="/selectResAdmin")
    public String selectResAdmin(@RequestParam String estateId, ModelMap model){
        List<Account> accountList=accountService.selectByInfoId(estateId);

        for (Account a:accountList){
            a.setEstate(estateService.selectById(estateId));
            a.setResidence(residenceService.selectById(a.getInfoId().split(",")[1]));
        }
        model.put("accountList",accountList);
        return  "manager/residence/resAdminList";
    }*/

    /**
     * 市内小区数量
     * @return
     */
   /* @RequestMapping(value = "/countName2")
    @ResponseBody
    public Map<String, Object> countName2(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Residence residence = new Residence();

        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute(Const.ACCOUNT);
        if (account.getType() == 2) {
            residence.setEstateId(account.getInfoId());
        }else if(account.getType() == 21){
            residence.setId(account.getInfoId().split(",")[1]);
        }else if(account.getType() == 5){
            residence.setBelong(account.getInfoId());
        }

        List<Residence> queryCommunityForList = residenceService.countName2(residence);
        int length = queryCommunityForList.size();
        String[] nameArray = new String[length];
        List<Echat> list = new ArrayList<Echat>();
        for(int i = 0 ;i<nameArray.length; i++  ){
            nameArray [i] = queryCommunityForList.get(i).getCity();  //获取市区
        }
        for(Residence community : queryCommunityForList){
            residence.setCity(community.getCity());
            Residence re = residenceService.countNumber2(residence);
            Echat echat= new Echat();
            echat.setValue(re.getCity());
            echat.setName(community.getCity());
            list.add(echat);
        }
        map.put("value",list);
        map.put("name",nameArray);
        return map;
    }
*/
    /**
     * 小区数据导出
     * @param request
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping(value = "/exportEcxel")
    @RequiresPermissions("residence:exportEcxel")
    public void exportEcxel(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        ParamMap paramMap = new ParamMap(request);
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        if(account.getType() == 2) {
            paramMap.put("estateId", account.getInfoId());
        }else if (account.getType() == 21){
            paramMap.put("residenceId",account.getInfoId().split(",")[1]);
        }else if(account.getType() == 5){
            paramMap.put("policeId", account.getInfoId());
        }
        List<Residence> list = residenceService.listWithoutPage(paramMap);
        String originFileUrl = request.getServletContext().getRealPath("/download/info.xlsx");
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Residence.class, list);
        FileOutputStream fos = new FileOutputStream(originFileUrl);
        workbook.write(fos);

        String fileName = "小区导出.xls";
        fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        //设置文件MIME类型
        response.setContentType(request.getServletContext().getMimeType(fileName));
        //设置Content-Disposition
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        //读取文件
        InputStream in = new FileInputStream(originFileUrl);
        OutputStream out = response.getOutputStream();

        //写文件
        int b;
        while((b=in.read()) != -1) {
            out.write(b);
        }
        fos.close();
        in.close();
        out.close();
    }

    /**
     * 小区数据excel导入
     * @param session
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/importExcel")
    @ResponseBody
    @RequiresPermissions("residence:importExcel")
    public Tip importExcel(HttpSession session, MultipartFile file) throws Exception {
        Tip tip = new Tip();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        if(account.getType() == 2) {
            List<Residence> list = ExcelImportUtil.importExcel(file.getInputStream(), Residence.class, new ImportParams());
            Integer index = 0;
            if(!list.isEmpty()) {
                for(Residence r : list) {
                    r.setId(UUIDFactory.getStringId());
                    r.setEstateId(account.getInfoId());
                    try {
                        residenceService.insert(r);
                        index ++;
                    } catch(Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                }
                tip.setSuccess(true);
                tip.setCode(0);
                tip.setData("查找到" + list.size() + "条数据，导入成功" + index + "条，导入失败" + (list.size()-index) + "条，导入完成");
            } else {
                tip.setSuccess(false);
                tip.setCode(2);
                tip.setData("导入信息为空，请检查上传的excel");
            }
        } else {
            tip.setSuccess(false);
            tip.setCode(1);
            tip.setData("只有物业管理员才可以导入小区信息");
        }
        return tip;
    }

    /**
     * 进入小区详情页面
     * @param modelMap 返回的实体容器
     * @return
     */
/*    @RequestMapping(value="/ResDetail")
    public String ResDetail(HttpServletRequest request, ModelMap modelMap, String residenceId, Integer page, Integer size){
        ParamMap paramMap=new ParamMap(request);
        paramMap.put("residenceId",residenceId);

        //小区人数
        Long count = userService.countId(residenceId);
        modelMap.put("count",count);
        //男性数量
        Long countMale = userService.countMale(residenceId);
        modelMap.put("countMale",countMale);
        //女性数量
        Long countFemale = userService.countFemale(residenceId);
        modelMap.put("countFemale",countFemale);
        //60以上
        Long countOld = userService.countOld(residenceId);
        modelMap.put("countOld",countOld);
        //16以下
        Long countChild = userService.countChild(residenceId);
        modelMap.put("countChild",countChild);

        //特殊人群总人数 sensitiveCount 1：涉恐人员，2：涉稳人员，3：重大刑事犯罪前科人员，4：涉毒人员，5：肇事肇祸精神病人6：重点上访人员
        Long sensitiveCount = userService.sensitiveCount(residenceId);
        modelMap.put("sensitiveCount",sensitiveCount);

        //涉恐人员人数 terrorismCount
        Long terrorismCount = userService.terrorismCount(residenceId);
        modelMap.put("terrorismCount",terrorismCount);
        paramMap.put("sensitiveStatus","1");
        List<Card> terrorism=cardService.selectBySensitive(paramMap);
        modelMap.put("terrorism",terrorism);

        //涉稳人员人数 stabilityCount
        Long stabilityCount = userService.stabilityCount(residenceId);
        modelMap.put("stabilityCount",stabilityCount);
        paramMap.put("sensitiveStatus","2");
        List<Card> stability=cardService.selectBySensitive(paramMap);
        modelMap.put("stability",stability);

        //重大刑事犯罪前科人员人数 criminalCount
        Long criminalCount = userService.criminalCount(residenceId);
        modelMap.put("criminalCount",criminalCount);
        paramMap.put("sensitiveStatus","3");
        List<Card> criminal=cardService.selectBySensitive(paramMap);
        modelMap.put("criminal",criminal);

        //涉毒人数 drugsCount
        Long drugsCount = userService.drugsCount(residenceId);
        modelMap.put("drugsCount",drugsCount);
        paramMap.put("sensitiveStatus","4");
        List<Card> drugs=cardService.selectBySensitive(paramMap);
        modelMap.put("drugs",drugs);

        //肇事肇祸精神病人人数 psychosisCount
        Long psychosisCount = userService.psychosisCount(residenceId);
        modelMap.put("psychosisCount",psychosisCount);
        paramMap.put("sensitiveStatus","5");
        List<Card> sensitive=cardService.selectBySensitive(paramMap);
        modelMap.put("sensitive",sensitive);

        //重点上访人员人数 petitionCount
        Long petitionCount = userService.petitionCount(residenceId);
        modelMap.put("petitionCount",petitionCount);
        paramMap.put("sensitiveStatus","6");
        List<Card> petition=cardService.selectBySensitive(paramMap);
        modelMap.put("petition",petition);

        //门禁卡
        Date currentDate=new Date();
        paramMap.put("currentDate",currentDate);
        String resId = residenceId.substring(0,19);
        paramMap.put("residenceId",resId);
        PageInfo<Card> pageInfo=cardService.listOfResidence(paramMap,page,size);
        modelMap.put("pageInfo",pageInfo);
        PageInfo<Card> pageTimeInfo = cardService.timeList(paramMap,page,size);
        modelMap.put("pageTimeInfo",pageTimeInfo);
        modelMap.put("residenceId", residenceId);
        return "manager/residence/ResDetail";
    }*/

    /**
     *
     */

    @RequestMapping(value = "/recordDoor")
    public String recordList(HttpServletRequest request, String egcardId, ModelMap modelMap, Integer page, Integer size) {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap = new ParamMap(request);
        if (account.getType() == 2) {
            paramMap.put("estateid", account.getInfoId());
        } else if (account.getType() == 21) {
            paramMap.put("residenceid", account.getInfoId().split(",")[1]);
        } else if (account.getType() == 5) {
            paramMap.put("policeId", account.getInfoId());
        }

        paramMap.put("egcardId",egcardId);
   /*     PageInfo<Record> pageInfo = recordService.selectByEgcardId3(paramMap, page, size);
        modelMap.put("pageInfo", pageInfo);*/
        modelMap.put("type", account.getType());
        modelMap.putAll(paramMap);
        return "manager/residence/recordDoor";
    }




    //---------------------------- property -------------------------------

    @Resource
    private ResidenceService residenceService;

 /*   @Resource
    private RecordService recordService;*/

    @Resource
    private CityService cityService;

    @Resource
    private BuildingService buildingService;

    @Resource
    private UnitService unitService;

    @Resource
    private RoomService roomService;

    @Resource
    private AccountService accountService;

    @Resource
    private AccountRoleService accountRoleService;

  /*  @Resource
    private EstateService estateService;

    @Resource
    private PoliceResidenceService policeResidenceService;

    @Resource
    private UserRoomService userRoomService;

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private EquipmentCardService equipmentCardService;

    @Resource
    private CardService cardService;

    @Resource
    private UserService userService;

    @Resource
    private ContentService contentService;

    @Resource
    private ReplyService replyService;*/
}
